package com.github.pig.stock.controller;

import com.aliyun.oss.model.OSSObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.ZyResult;
import com.github.pig.stock.common.config.AliyunOssConfig;
import com.github.pig.stock.common.enums.OrderAction;
import com.github.pig.stock.entity.TblStock;
import com.github.pig.stock.model.dto.StockInDTO;
import com.github.pig.stock.model.dto.StockSearchDTO;
import com.github.pig.stock.service.ITblStockService;
import com.github.pig.stock.service.OssService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author J神
 * @date 2018/5/11
 **/
@RestController
@RequestMapping("order")
public class StockController{
    @Autowired
    private ITblStockService tblStockService;
    @Autowired
    private OssService ossService;
    @Autowired
    private AliyunOssConfig aliyunOssConfig;
    /**
     * 批量导入
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "批量导入")
    @PutMapping
    public ZyResult importStocks(String key) throws Exception {
        OSSObject ossObject=ossService.getOssClient().getObject(aliyunOssConfig.getBucketName(),key);
        return ZyResult.ok(tblStockService.readStockInExecl(ossObject.getObjectContent()));
    }

    /**
     * 修改
     *
     * @return
     */
    @ApiOperation(
            value = "修改"
    )
    @PostMapping
    public ZyResult update(@RequestBody StockInDTO stockInDTO) {
        if(stockInDTO.getOrderAction()== OrderAction.DISTRIBUTE){
            return ZyResult.ok(tblStockService.distribute(stockInDTO));
        }
        if(stockInDTO.getOrderAction()==OrderAction.GATHERING){
            return ZyResult.ok(tblStockService.gathering(stockInDTO));
        }
        return ZyResult.error();
    }

    /**
     * @return
     */
    @ApiOperation(
            value = "列表页检索"
    )
    @PostMapping("orderPage")
    public ZyResult search(@RequestBody StockSearchDTO stockSearchDTO) {
        Page<TblStock> pages=tblStockService.search(stockSearchDTO);
        return ZyResult.ok(pages);
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    public ZyResult detail() {
        return ZyResult.ok();
    }

    @PostMapping("export")
    public void getFile(@RequestBody StockSearchDTO stockSearchDTO,HttpServletRequest request, HttpServletResponse response) throws IOException {
//        Page<TblStock> pages=tblStockService.search(stockSearchDTO);
        XSSFWorkbook workBook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet sheet = workBook.createSheet("helloWorld");
        //创建行
        XSSFRow row = sheet.createRow(2);
        //创建单元格，操作第三行第三列
        XSSFCell cell = row.createCell(2, XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("helloWorld");
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String disposition = "attachment;filename="+java.net.URLEncoder.encode("person.xls", "UTF-8");
        response.setHeader("Content-Disposition", disposition);// 设定输出文件头
        response.setContentType("application/msexcel"); // 设定输出类型
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workBook.write(baos);
        baos.flush();
        byte[] aa = baos.toByteArray();
        InputStream is = new ByteArrayInputStream(aa, 0, aa.length);
        // 得到响应对象的输出流,用于向客户端输出二进制数据
        ServletOutputStream sos = response.getOutputStream();
        byte[] data = new byte[2048];
        int len = 0;
        while ((len = is.read(data)) > 0) {
            sos.write(data, 0, len);
        }
        is.close();
        sos.close();
        baos.close();
    }

}
